<template>
  <div
      class="callback float"
      @click="onClick"
      @mousedown="down"
      @touchstart="down"
      @mousemove="move"
      @touchmove="move"
      @mouseup="end"
      @touchend="end"
      ref="fu"
  >
    <img @click="callback" src="../../../public/image/addFlog.jpg" alt />
  </div>
    <el-dialog
        v-model="dialogVisible"
        title="新增奖项"
        width="70%"
    >
      <div class="Table">
        <div class="form">
          <div class="form-body">
            <el-form :model="award" :inline="true" :rules="rules" ref="awardForm">
              <el-form-item label="奖项名称" prop="title">
                <el-input
                    v-model="award.title"
                    type="text"
                    autocomplete="off"
                    placeholder="请输入奖项名称"
                />
              </el-form-item>
              <el-form-item label="奖项数量" label-width="120px" prop="number">
                <el-input-number v-model="award.number" :min="1" :max="10"/>
              </el-form-item>
              <el-form-item label="奖项金额" label-width="120px" prop="money">
                <el-input
                    v-model="award.money"
                    type="text"
                    autocomplete="off"
                    placeholder="请输入奖项金额"
                ></el-input>
              </el-form-item>
              <el-form-item label="审核老师" prop="userId">
                <el-select v-model="award.userId" placeholder="选择审核老师">
                  <el-option
                      v-for="item in teacherOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="绩点(前%)" label-width="120px">
                <el-input-number v-model="award.standard" :step="2" />
              </el-form-item>
              <el-form-item label="奖项描述" label-width="120px" prop="explain">
                <el-input
                    v-model="award.explain"
                    type="textarea"
                    autocomplete="off"
                    placeholder="请输入奖项描述"
                ></el-input>
              </el-form-item>
              <el-form-item label="其他要求" prop="details">
                <el-input
                    v-model="award.details"
                    type="text"
                    autocomplete="off"
                    placeholder="请补充其他要求(如四六级)"
                />
              </el-form-item>
              <el-form-item label="截止日期" label-width="120px" prop="deadline">
                <el-date-picker
                    v-model="award.deadline"
                    type="datetime"
                    placeholder="请选择截止日期"
                />
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="toAddAward">
            新增
          </el-button>
          <el-button @click="dialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script>
import {addAward, ifAwardTitle} from "../../api/award";
import {getTeacherOptions} from "../../api/user";
import {ElMessage} from "element-plus";
export default {
  name: "suspensionBall",
  data() {
    return {
      award:{
        title:'',
        number:'',
        userId:'',
        deadline:'',
        details:'',
        money:'',
        standard:'',
        explain:''
      },//奖项表单
      rules: {
        title: [{ required: true, message: '请输入奖项名称', trigger: 'blur' }],
        money: [{ required: true, message: '请输入奖项金额', trigger: 'blur' }],
        userId: [{ required: true, message: '请选择审核老师', trigger: 'blur' }],
        number: [{ required: true, message: '请选择奖项名额', trigger: 'blur' }],
        explain: [{ required: true, message: '没有请填"无"', trigger: 'blur' }],
        details: [{ required: true, message: '没有请填"无"', trigger: 'blur' }],
        deadline: [{ required: true, message: '请选择一个日期', trigger: 'blur' }],

      },//表单校验
      teacherOptions:'',//老师列表
      dialogVisible:false,
      isLoading: false,
      flags: false, //控制使用
      position: {
        x: 0,
        y: 0,
      },
      nx: "",
      ny: "",
      dx: "",
      dy: "",
      xPum: "",
      yPum: "",
    };
  },
  created() {
    this.selectTeacherOptions();
  },
  methods: {
    //获得老师和Id对应关系
    selectTeacherOptions(){
      getTeacherOptions().then(response =>{
        this.teacherOptions=response.data;
      })
    },
    //新增奖项
    toAddAward(){
      this.$refs['awardForm'].validate((valid) => {
        //表单合法
        if (valid){
          this.award.number = this.award.number+"";
          this.award.standard = this.award.standard+"";
          ifAwardTitle(this.award).then(res => {
            if (res.message === '奖项存在')
            {
              ElMessage.error('奖项名称已存在')
              return
            }else
            {
              addAward(this.award).then(res =>{
                this.dialogVisible = false;
                window.history.go(0);
                this.$notify({
                  title: '成功',
                  message: "奖项"+res.message,
                  type: 'success'
                });
              })
            }
          })
        }
        //表单不合法
        else{
          return false;
        }
      })
    },
    callback() {
      this.dialogVisible = true;
    },
    onRefresh() {
      // window.location.reload();
      setTimeout((res) => {
        console.log(res);
        this.isLoading = false;
      }, 1000);
    },
    down() {
      this.flags = true;
      var touch;
      if (event.touches) {
        touch = event.touches[0];
      } else {
        touch = event;
      }
      this.position.x = touch.clientX;
      this.position.y = touch.clientY;
      this.dx = this.$refs.fu.offsetLeft;
      this.dy = this.$refs.fu.offsetTop;
    },
    move() {
      if (this.flags) {
        var touch;
        if (event.touches) {
          touch = event.touches[0];
        } else {
          touch = event;
        }
        this.nx = touch.clientX - this.position.x;
        this.ny = touch.clientY - this.position.y;
        this.xPum = this.dx + this.nx;
        this.yPum = this.dy + this.ny;
        let width = window.innerWidth - this.$refs.fu.offsetWidth; //屏幕宽度减去自身控件宽度
        let height = window.innerHeight - this.$refs.fu.offsetHeight; //屏幕高度减去自身控件高度
        this.xPum < 0 && (this.xPum = 0);
        this.yPum < 0 && (this.yPum = 0);
        this.xPum > width && (this.xPum = width);
        this.yPum > height && (this.yPum = height);
        // if (this.xPum >= 0 && this.yPum >= 0 && this.xPum<= width &&this.yPum<= height) {
        this.$refs.fu.style.left = this.xPum + "px";
        this.$refs.fu.style.top = this.yPum + "px";
        // }
        //阻止页面的滑动默认事件
        document.addEventListener(
            "touchmove",
            function () {
              event.preventDefault();
            },
            false
        );
      }
    },
    //鼠标释放时候的函数
    end() {
      this.flags = false;
    },
    onClick() {
      //在这里我是作为子组件来使用的
      this.$emit("click");
    },
    //奖项截止时间格式

  },
};
</script>
<style scoped>
.callback p {
  font-size: 16px;
  color: #fff;
  background: rgba(56, 57, 58, 0.5);
  border-radius: 50%;
  text-align: center;
  width: 50px;
  height: 50px;
  line-height: 50px;
  font-family: PingFang SC;
  font-weight: 600;
  box-shadow: 0 0 10px #fff;
}
.callback img {
  display: block;
  width: 50px;
  height: 50px;
  box-shadow: 0 0 10px rgb(133, 129, 129);
  border-radius: 50%;
  background: #fff;
}
.callback {
  position: fixed;
  top: 40px;
  left: 20px;
  z-index: 99999;
}
.float {
  position: fixed;
  right: 20px;
  top: 60%;
  touch-action: none;
  text-align: center;
  width: 50px;
  height: 50px;
  border-radius: 24px;
  line-height: 48px;
  color: white;
}
.Table {
  display: flex;
  justify-content: center;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(0, 0, 0, 0.15); /* 灰色 */
}
.form {
  display: flex;
  flex-direction: column;
}
.form-body{
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>

